package com.yc.game.service.impl;

import com.yc.game.dao.MoneyMapper;
import com.yc.game.pojo.Money;
import com.yc.game.pojo.User;
import com.yc.game.util.MD5Util;
import com.yc.game.dao.UserMapper;
import com.yc.game.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MoneyMapper moneyMapper;

    /**
     * 用户注册
     * @param user
     * @return
     */
    @Override
    public int register(User user) {
        User newUser  = new User();
        newUser.setEmail(user.getEmail());
        newUser.setPhone(user.getPhone());
        newUser.setUserName(user.getUserName());
        newUser.setStatus(1);
        newUser.setType(1);
        newUser.setPassword(MD5Util.MD5EncodeUtf8(user.getPassword()));
        // 初始化保证金
        Money money = new Money();
        money.setMoney(0.0);
        money.setEmail(user.getEmail());
        money.setStatus(1);
        moneyMapper.insertSelective(money);
        return userMapper.insertSelective(newUser);
    }

    /**
     * 检查用户是否被注册
     * @param email
     * @return
     */
    @Override
    public int getByEmail(String email) {

        return userMapper.getByEmail(email);
    }

    /**
     * 用户登录
     * @return
     */
    @Override
    public User getUserByEmail(User user) {
        return userMapper.getUserByEmail(user.getEmail(), MD5Util.MD5EncodeUtf8(user.getPassword()));
    }
}
